from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializers.sku_serializers import *
from meiduo_admin.paginations import MyPage



class SKUGoodsView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializer

    pagination_class = MyPage

    def get_queryset(self):
        keywork = self.request.query_params.get('keyword')
        if keywork:
            return self.queryset.filter(name__contains=keywork)
        return self.queryset.all()

#更新SKU
class SKUCategorieView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = GoodsCateSimpleSerializer


#新建SKU
class SPUSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

#可选规格
class SPUSpecView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecSimpleSerializer

    def get_queryset(self):
        spu_id = self.kwargs.get('pk')
        #过滤出当前spu关联的所有的规格查询集返回
        return self.queryset.filter(spu_id=spu_id)

